<?php 
/**
 * 分类表模板
 */
class CategoryModel extends Model{
	
	public $table = 'category';
	//自动验证
	public $validate = array(
		array('cname','nonull','分类名不能为空',2,3),
		array('csort','noull','分类排序不能为空',2,3),
		array('csort','num:0,65535','请输入数字范围为0-6535',2,3),
		array('cname','maxlen:60','分类名太长',2,3),
	);
	/**
	 * 添加分类
	 */
	public function addCate(){
		//执行自动验证
		if(!$this->create()) return false;
		$this->add();
		return true;
		
	}
	/**
	 * 添加子分类
	 */
	public function addSon(){
		$this->validate = array(
			array('cname','nonull','分类名不能为空',2,3),
			array('csort','noull','分类排序不能为空',2,3),
			array('csort','num:0,65535','请输入数字范围为0-6535',2,3),
			array('Type_tid','nonull','未选择类型',2,3),
			array('cname','maxlen:60','分类名太长',2,3),
		);
		//执行自动验证
		if(!$this->create()) return false;
		$this->add();
		return true; 
		
	}
	
	/**
	 * 编辑分类
	 */
	public function edit($cid){
		$this->validate = array(
			array('cname','nonull','分类名不能为空',2,3),
			array('csort','noull','分类排序不能为空',2,3),
			array('csort','num:0,65535','请输入数字范围为0-6535',2,3),
			array('Type_tid','nonull','未选择类型',2,3),
			array('cname','maxlen:60','分类名太长',2,3),
		);
		//如果和是顶级分类就删除Type_tid的自动验证
		if(Q('post.pid')==0){
			unset($this->validate[3]);
		}
		if(!$this->create()) return false;
		$this->where("cid={$cid}")->update();
		return true;
	}
	
	
	/**
	 * 获取子分类cid
	 */
	public function getSon($cate,$cid){
		$temp = array();
		foreach ($cate as $v) {
			if($v['pid'] == $cid){
				$temp[] = $v['cid'];
				$temp = array_merge($temp,$this->getSon($cate,$v['cid']));
			}
			
		}
		//获取的是 cid下的所有子集cid
		return $temp;
	}
	

	

	
	
	
}

















 ?>